﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors;
using DevExpress.XtraPivotGrid;
using DevTeam;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Planlama
{
    public partial class UretimOzet : XForm
    {
        public UretimOzet()
        {
            InitializeComponent();
        }

        private void UretimOzet_Load(object sender, EventArgs e)
        {
            

            beiBaslangic.EditValue = new DateTime(2015, 1, 1);
            beiBitis.EditValue = DateTime.Today;

            RefreshData();
            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);

            pvg.OptionsPrint.PrintColumnHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintDataHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.MergeRowFieldValues = false;
            pvg.OptionsPrint.MergeColumnFieldValues = false;

            splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            splitContainerControl1.AutoSize();

            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshData();
        }

        BindingSource bs;
        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        void RefreshData()
        {
            gridView1.EndEditing();
            bs = gridView1.Init("GetUretimOzet", beiBaslangic.EditValue.ToDate(), beiBitis.EditValue.ToDate());

            InitPivot(pvg, bs);

            pvg.Cells.Selection = new Rectangle(new Point(0, 0), new Size(1, 1));
            //pvg.Fields["ProductId"].BestFit();
            pvg.BestFit();

        }

        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pvg, BindingSource bs)
        {
            pvg.DataSource = bs;

            if (!PivotInitialized)
            {
                pvg.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                pvg.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                pvg.CellSelectionChanged += new EventHandler(pvg_CellSelectionChanged);
                pvg.FocusedCellChanged += new EventHandler(pvg_FocusedCellChanged);
                pvg.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
                pvg.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);

                pvg.BeginUpdate();

                //new field - toplam tutar  (metre x fiyat+dvz cinsi)

                AddNewField(pvg, "CustomerId", PivotArea.RowArea).FieldEdit = RI(Rp.V_Customer);
                AddNewField(pvg, "AtelyeId", PivotArea.RowArea).FieldEdit = RI(Rp.Atelye);
                
                PivotGridField[] fields1 = new PivotGridField[2];
                fields1[0] = pvg.Fields["AtelyeId"];
                fields1[1] = pvg.Fields["CustomerId"];
                pvg.Groups.Add(fields1, "AtelyeMusteri");


                PivotGridField[] fields2 = new PivotGridField[3];

                PivotGridField fieldYear = new PivotGridField("Tarih", PivotArea.ColumnArea);
                PivotGridField fieldMonth = new PivotGridField("Tarih", PivotArea.ColumnArea);
                PivotGridField fieldWeek = new PivotGridField("Tarih", PivotArea.ColumnArea);
                //PivotGridField fieldDay = new PivotGridField("Tarih", PivotArea.ColumnArea);

                // Add the fields to the field collection.
                pvg.Fields.Add(fieldYear);
                fieldYear.GroupInterval = PivotGroupInterval.DateYear;
                fieldYear.Caption = "Yıl";

                pvg.Fields.Add(fieldMonth);
                fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
                fieldMonth.Caption = "Ay";

                pvg.Fields.Add(fieldWeek);
                fieldWeek.GroupInterval = PivotGroupInterval.DateWeekOfYear;
                fieldWeek.Caption = "Hafta";

                //pvg.Fields.Add(fieldDay);
                //fieldDay.GroupInterval = PivotGroupInterval.DateDay;
                //fieldDay.Caption = "Gün";

                fields2[0] = fieldYear;
                fields2[1] = fieldMonth;
                fields2[2] = fieldWeek;

                pvg.Groups.Add(fields2, "Tarih");
                AddNewField(pvg, "Kalan", PivotArea.DataArea);
                AddNewField(pvg, "UretimeCikan", PivotArea.DataArea);
                AddNewField(pvg, "Siparis", PivotArea.DataArea);

                /*
                 * https://www.devexpress.com/Support/Center/e/T191674.aspx
                 */
                 
                // Creates a new FormatRule object and adds to the collection.
                PivotGridFormatRule newRule = new PivotGridFormatRule();
                pvg.FormatRules.Add(newRule);

                // Sets a Measure.
                newRule.Measure = pvg.Fields["Siparis"];

                // Creates a new Settings object.
                FormatRuleFieldIntersectionSettings newClass = new FormatRuleFieldIntersectionSettings();
                newRule.Settings = newClass;

                // Sets the Settings parameters.
                newClass.Column = pvg.Fields["Tarih"];
                newClass.Row = pvg.Fields["AtelyeId"];

                // Creates a new Rule object and defines its type.
                FormatConditionRuleDataBar formatConditionRuleDataBar = new FormatConditionRuleDataBar();
                newRule.Rule = formatConditionRuleDataBar;

                // Sets the Rule parameters.
                formatConditionRuleDataBar.PredefinedName = "Yellow Gradient";
                


                //.Width = 40;  .Visible = true;
                //    fld.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;

                //foreach (PivotGridField pgf in pvg.Fields)
                //{
                //    pgf.SortOrder = PivotSortOrder.Ascending;
                //    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                //}
                pvg.EndUpdate();

                pvg.BestFit();
                fields2[1].ExpandedInFieldsGroup = fields2[2].ExpandedInFieldsGroup = false;
                fields1[0].ExpandedInFieldsGroup = false;

                PivotInitialized = true;
            }
        }


        void pvg_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }


        void pvg_CellSelectionChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        private void CellChanged()
        {
            double PivotSum = 0.0;

            if (pvg.Cells.MultiSelection.SelectedCells.Count > 0)
            {
                foreach (Point cell in pvg.Cells.MultiSelection.SelectedCells)
                {
                    DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetCellInfo(cell.X, cell.Y);
                    if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                        cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                        PivotSum += Convert.ToDouble(pvg.GetCellValue(cell.X, cell.Y));
                }
            }
            else
            {
                DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetFocusedCellInfo();
                if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                    cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                    PivotSum += Convert.ToDouble(pvg.GetCellValue(pvg.Cells.FocusedCell.X, pvg.Cells.FocusedCell.Y));

            }
            //Notify(PivotSum == 0 ? string.Empty : String.Format("{0:#,##0.## mt'.'}", PivotSum));
            Notify(PivotSum == 0 ? string.Empty : String.Format("{0:#,##0}", PivotSum));
        }

        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl1.DataSource = e.CreateDrillDownDataSource();
            //gridView1.MakeColumnInvisible("Name");
            ExpandCollapse(splitContainerControl1, (gridView1.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            splitContainerControl1.PanelVisibility = (gridView1.RowCount > 0 ? DevExpress.XtraEditors.SplitPanelVisibility.Both: DevExpress.XtraEditors.SplitPanelVisibility.Panel1);

            gridView1.BestFitColumns();
            gridView1.MoveFirst();
        }
    }
}
